﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace NetFrameworkExtensions.BusinessLayer
{
  [Serializable]
  [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
  public class IsContentPropertyAttribute : Attribute
  {
    public static IEnumerable<PropertyInfo> FindProperties<T>()
    {
      return FindProperties(typeof(T));
    }

    public static IEnumerable<PropertyInfo> FindProperties(Type type)
    {
      if (type == null) throw new ArgumentNullException("type");
      var childProps = type
        .GetProperties()
        .Where(p => p.CanRead && p.GetIndexParameters().Length == 0 && p.GetCustomAttributes(typeof(IsContentPropertyAttribute), true).Any())
        .ToArray();
      return childProps;
    }

    public static IEnumerable<object> GetContent(object item)
    {
      if (item == null) yield break;
      foreach (var prop in FindProperties(item.GetType()))
      {
        var value = prop.GetValue(item, null);
        yield return value;
      }
    }
  }
}
